# -*- Makefile -*- for Dungeon Crawl (unix)

#
# Modified for Crawl Reference by $Author: dshaligram $ on $Date: 2007-06-24T16:27:58.475101Z $
#

GAME = crawl

# this file contains a list of the libraries.
# it will make a variable called OBJECTS that contains all the libraries
include makefile.obj

OBJECTS += libgui.o tilepick.o tile2.o tilereg.o tilesdl.o tilefont.o tiletex.o tilemcache.o tilebuf.o crash-u.o

SDL_CFLAGS := $(shell sdl-config --cflags)
SDL_LDFLAGS := $(shell sdl-config --libs) -lSDLmain

CXX = g++
DELETE = rm -f
COPY = cp
OS_TYPE = UNIX
EXTRA_INCLUDES := -DUSE_TILE -DCLUA_BINDINGS
# To get stack trace symbols.
LDFLAGS = -rdynamic

# Change this to y if you want to use Unicode glyphs in the map, and you have
# libncursesw available.
UNICODE_GLYPHS = n

ifeq ($(strip $(OSX)),y)
PNG_INCLUDE := -I/sw/include
PNG_LIB := -L/sw/lib
FREETYPE_INCLUDE := -I/sw/include/freetype2

# 10.5 Fink bug
FINK_OPENGL_WORKAROUND := -Wl,-dylib_file,/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib:/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib

SDL_LDFLAGS += -framework Cocoa -L/usr/X11/lib $(FINK_OPENGL_WORKAROUND)

EXTRA_INCLUDES += -I/sw/include
else
PNG_INCLUDE :=
PNG_LIB :=
FREETYPE_INCLUDE := -I/usr/include/freetype2
endif

# If you have lex and yacc, set DOYACC to y (lowercase y).
DOYACC := y

# Permissions to set on the game executable.
MCHMOD := 2755

# Permissions to set on the save directory.
MCHMOD_SAVEDIR := 775

# The user:group to install the game as.
INSTALL_UGRP := games:games

INSTALLDIR   := /usr/games/crawl

# If you're installing Crawl for multiple users, you *must* set this to a
# valid path before building Crawl. This is not necessary if you are building
# Crawl for a single user.

# SAVEDIR := /usr/games/crawl/saves/
# DATADIR := /usr/games/crawl/data/

LEX  := flex
YACC := bison -y

LUASRC := util/lua/src
LUALIB = lua
LUALIBA = l$(LUALIB).a

SQLSRC   := util/sqlite
SQLLIB   := sqlite3
SQLLIBA  := lib$(SQLLIB).a
FSQLLIBA := $(SQLSRC)/$(SQLLIBA)

LIBDBM         := -L$(SQLSRC) -l$(SQLLIB)
EXTRA_INCLUDES += -I$(SQLSRC)
EXTRA_DEPENDS  += $(FSQLLIBA)

RLTILES = rltiles

EXTRA_INCLUDES += -I$(RLTILES)

TILEDEFS = dngn main player
TILEDEFPRES = $(TILEDEFS:%=$(RLTILES)/tiledef-%)
TILEDEFOBJS = $(TILEDEFPRES:%=%.o)
TILEDEFSRCS = $(TILEDEFPRES:%=%.cc)
TILEDEFHDRS = $(TILEDEFPRES:%=%.h)

OBJECTS += $(TILEDEFOBJS)

TILEFILES = \
	main.png \
	player.png \
    dngn.png
DESTTILEFILES = $(TILEFILES:%=dat/tiles/%)

EXTRA_DEPENDS += $(DESTTILEFILES)

LIB = -L$(LUASRC) -l$(LUALIB) $(LIBDBM) -lpng $(SDL_LDFLAGS) -lGL -lGLU -lSDL_image -lfreetype

INCLUDES := $(INCLUDES) -Iutil -I. -I$(LUASRC) $(EXTRA_INCLUDES) $(PNG_INCLUDE) $(FREETYPE_INCLUDE)

CFWARN := -Wall -Wwrite-strings -Wshadow -pedantic -Wno-parentheses

CFOTHERS := -fsigned-char -D$(OS_TYPE) $(EXTRA_FLAGS) -DCLUA_BINDINGS

ifneq ($(HURRY),y)
ifneq ($(DEBUG_CRAWL),y)
CFWARN += -Wuninitialized
CFOTHERS += -O2
endif
endif

ifneq ($(strip $(SAVEDIR)),)
CFOTHERS += '-DSAVE_DIR_PATH="$(strip $(SAVEDIR))"'
endif

ifneq ($(strip $(DATADIR)),)
CFOTHERS += '-DDATA_DIR_PATH="$(strip $(DATADIR))"'
endif

CFLAGS  := $(INCLUDES) $(CFWARN) $(CFOTHERS) $(SDL_CFLAGS)
YCFLAGS := $(INCLUDES) $(CFOTHERS)

UTIL = util/

YTABC  := levcomp.tab.c
YTABH  := levcomp.tab.h

OBJECTS := $(UTIL)levcomp.tab.o $(UTIL)levcomp.lex.o $(OBJECTS)

ifeq ($(strip $(LEX)),)
DOYACC :=
endif

ifeq ($(strip $(YACC)),)
DOYACC :=
endif

GAME_DEPENDS := $(LUASRC)$(LUALIBA) $(EXTRA_DEPENDS) $(OBJECTS)
SRC_PKG_BASE := stone_soup
SRC_VERSION  := $(shell egrep 'VER_NUM  *".*"' version.h | \
			egrep -o '[0-9]\.[0-9](\.[0-9])?')
PKG_SRC_DIR  := $(SRC_PKG_BASE)-$(SRC_VERSION)-src
SRC_PKG_TAR  := $(PKG_SRC_DIR).tbz2
SRC_PKG_ZIP  := $(PKG_SRC_DIR).zip

PKG_TIDY_LIST := $(UTIL)*.o $(LEVCOMP) *.o \
		$(UTIL)*.tab.cc $(UTIL)*.tab.h $(UTIL)*.lex.cc *.ixx
PKG_EXCLUDES  := $(PWD)/misc/src-pkg-excludes.lst

##########################################################################

all: $(GAME)

##########################################################################
# Dependencies

DEPENDENCY_MKF := makefile.dep

depend: $(OBJECTS:.o=.cc)
	rm -f $(DEPENDENCY_MKF).tmp
	@for i in $^; do \
	    echo "Updating dependencies for $$i"; \
	    $(CXX) -MM $(CFLAGS) $$i >>$(DEPENDENCY_MKF).tmp 2>/dev/null; \
	done
	mv -f $(DEPENDENCY_MKF).tmp $(DEPENDENCY_MKF)

-include $(DEPENDENCY_MKF)

##########################################################################
# The level compiler
#

ifeq ($(DOYACC),y)

prebuildyacc:	$(UTIL)levcomp.tab.cc $(UTIL)levcomp.tab.h $(UTIL)levcomp.lex.cc
		$(COPY) $^ prebuilt/

$(UTIL)levcomp.tab.cc: $(UTIL)levcomp.ypp
		cd $(UTIL) && $(YACC) -d -b levcomp levcomp.ypp \
			   && mv $(YTABC) levcomp.tab.cc || false

$(UTIL)levcomp.lex.cc: $(UTIL)levcomp.lpp
		cd $(UTIL) && $(LEX) -olevcomp.lex.cc levcomp.lpp

else

# Pull the level-compiler stuff up from prebuilt/

$(UTIL)levcomp.tab.cc: prebuilt/levcomp.tab.cc
		$(COPY) prebuilt/*.h $(UTIL)
		$(COPY) $< $@


$(UTIL)levcomp.lex.cc: prebuilt/levcomp.lex.cc
		$(COPY) $< $@

endif

##########################################################################
# RLTiles
#

ORIGTILEFILES = $(TILEFILES:%=$(RLTILES)/%)

makerltiles:
	cd $(RLTILES) && make -f makefile.unix all

$(TILEDEFSRCS): makerltiles

$(TILEDEFHDRS): makerltiles

$(ORIGTILEFILES): makerltiles

dat/tiles/%.png: $(RLTILES)/%.png
	$(COPY) $< $@

clean-rltiles:
	cd $(RLTILES) && make -f makefile.unix distclean && cd ..

##########################################################################


##########################################################################
# The actual build targets
#

install: $(GAME)
ifeq ($(DATADIR),)
	$(error DATADIR not set! Set DATADIR and run make clean install again)
endif
	[ -d $(INSTALLDIR) ] || mkdir -p $(INSTALLDIR)
	$(COPY) $(GAME) $(INSTALLDIR)
	chown $(INSTALL_UGRP) $(INSTALLDIR)/$(GAME)
	chmod ${MCHMOD} ${INSTALLDIR}/$(GAME)
	mkdir -p $(DATADIR)/dat
	mkdir -p $(DATADIR)/dat/lua
	mkdir -p $(DATADIR)/dat/clua
	mkdir -p $(DATADIR)/dat/descript
	mkdir -p $(DATADIR)/dat/database
	$(COPY) dat/*.des $(DATADIR)/dat
	$(COPY) dat/lua/*.lua $(DATADIR)/dat/lua
	$(COPY) dat/clua/*.lua $(DATADIR)/dat/clua
	$(COPY) dat/descript/*.txt $(DATADIR)/dat/descript
	$(COPY) dat/database/*.txt $(DATADIR)/dat/database
	mkdir -p $(DATADIR)/settings
	$(COPY) ../settings/* $(DATADIR)/settings/
	mkdir -p $(DATADIR)/docs
	$(COPY) ../docs/*.txt $(DATADIR)/docs
	mkdir -p $(DATADIR)/dat/tiles
	$(COPY) dat/tiles/* $(DATADIR)/dat/tiles
	chown -R $(INSTALL_UGRP) $(DATADIR)
ifneq ($(SAVEDIR),)
	mkdir -p $(SAVEDIR)
	chown $(INSTALL_UGRP) $(SAVEDIR)
	chmod $(MCHMOD_SAVEDIR) $(SAVEDIR)
endif

clean:
	$(DELETE) *.o
	$(DELETE) $(UTIL)*.o
	$(DELETE) $(LEVCOMP)
	$(DELETE) $(UTIL)*.tab.cc $(UTIL)*.tab.c $(UTIL)*.tab.h $(UTIL)*.lex.cc
	$(DELETE) *.ixx

clean-lua:
	cd $(LUASRC) && $(MAKE) clean

clean-sql:
	cd $(SQLSRC) && $(MAKE) clean

distclean: clean clean-lua clean-sql clean-rltiles
	$(DELETE) bones.*
	$(DELETE) morgue.txt
	$(DELETE) scores
	$(DELETE) $(GAME)
	$(DELETE) *.sav
	$(DELETE) core
	$(DELETE) *.0*
	$(DELETE) *.lab
	$(DELETE) $(DEPENDENCY_MKF)

$(GAME): $(GAME_DEPENDS)
	${CXX} ${LDFLAGS} $(CFLAGS) $(OBJECTS) -o $(GAME) $(LIB)

.PHONY : debug

debug: $(GAME)

profile: $(GAME_DEPENDS)
	${CXX} -g -p ${LDFLAGS} $(CFLAGS) $(OBJECTS) -o $(GAME) $(LIB)

%.o: %.cc $(TILEDEFHDRS)
	${CXX} ${CFLAGS} -c $< -o $@

# STL is bad in debug.  Always compile this file with optimizations.
tilebuf.o: tilebuf.cc $(TILEDEFHDRS)
	${CXX} ${CFLAGS} -O2 -c $< -o $@

# [ds] Note we don't use the standard CFLAGS here; that's intentional, most
# flex/bison combos I've tried don't produce code that passes the warnings
# test.
$(UTIL)%.o: $(UTIL)%.cc $(TILEDEFHDRS)
	$(CXX) $(YCFLAGS) -o $@ -c $<

#############################################################################
# Build Lua

$(LUASRC)$(LUALIBA):
	echo Building Lua...
	cd $(LUASRC) && $(MAKE) crawl_unix

#############################################################################
# Build SQLite

$(FSQLLIBA):
	echo Building SQLite
	cd $(SQLSRC) && $(MAKE)

#############################################################################
# Packaging a source tarball for release
#

# To package, you *must* have lex and yacc to generate the intermediates.
ifeq ($(DOYACC),y)
package-source: distclean prebuildyacc pkgtidy depend removeold vlink \
				pkgtarbz2 pkgzip

pkgtidy:
	$(DELETE) $(PKG_TIDY_LIST)

removeold:
	if [ -f ../../$(SRC_PKG_TAR) ]; then $(DELETE) ../../$(SRC_PKG_TAR); fi
	if [ -f ../../$(SRC_PKG_ZIP) ]; then $(DELETE) ../../$(SRC_PKG_ZIP); fi

# [ds] Existing directory names could produce a bad package!
vlink:
	cd .. && WHERE=$$PWD && cd .. && \
		( [ -e $(PKG_SRC_DIR) ] || ln -sf $$WHERE $(PKG_SRC_DIR) )

pkgtarbz2:
	cd ../.. && tar -ch --bzip2 -f $(SRC_PKG_TAR) \
		-X $(PKG_EXCLUDES) $(PKG_SRC_DIR)

pkgzip:
	cd ../.. && zip -rq $(SRC_PKG_ZIP) $(PKG_SRC_DIR) \
		-x@$(PKG_EXCLUDES)

endif
